﻿using DevExpress.Utils;
using DevExpress.XtraPrinting.Native;
using EyFramework.Common;
using EyFramework.Common.Utils;
using EyFramework.Library;
using Ncov2019.Core.Service;
using Ncov2019.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Ncov2019
{
    public partial class FrmNcov : FrmDevBase
    {
        private List<GridColumnInfo> columnInfos = new List<GridColumnInfo>();

        private INcovBarConfirmService ncovBarConfirmService;

        public FrmNcov()
        {
            InitializeComponent();
        }

        private void InitForm()
        {
            gvData.ShowRowNum();
            gcData.ShowContextMenu(this.components);//显示右键导出功能
            #region
            columnInfos.Add(new GridColumnInfo("BINGRENID", "病历号码", 100) {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("XINGMING", "姓名", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("CHUSHENGRQ", "出生日期", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("XB", "性别", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("NIANLING", "年龄", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("NLDW", "年龄单位", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("SHENFENZH", "身份证号", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("JIATINGDZ", "家庭地址", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("DIANHUAHM", "联系电话", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("HEALTHCODE", "健康码", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("KAIDANYSID", "开单医生代码", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("KAIDANYSXM", "开单医生名称", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("KAIDANKSID", "开单科室ID", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("KAIDANKSMC", "开单科室名称", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("ZHENDUANMC", "诊断名称", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("ZHUSU", "主诉", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("TIGEJC", "症状体征", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("JIANYAOBS", "简要病史", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("LIUXINGBINGXS", "流行病学史", 100)
            {
                COL_READONLY = false
            });
            //columnInfos.Add(new GridColumnInfo("JKM", "健康码", 100));
            columnInfos.Add(new GridColumnInfo("JIUZHENRQ", "就诊日期", 100)
            {
                COL_READONLY = false,
                Formattype = FormatType.DateTime,
                Formatstring = "yyyy-MM-dd HH:mm:ss"
            });
            columnInfos.Add(new GridColumnInfo("ZHUYUANRQ", "入院日期", 100)
            {
                COL_READONLY = false,
                Formattype = FormatType.DateTime,
                Formatstring = "yyyy-MM-dd HH:mm:ss"
            });
            columnInfos.Add(new GridColumnInfo("JIANCERID", "检测人ID", 100));
            columnInfos.Add(new GridColumnInfo("JIANCERXM", "检测人姓名", 100));
            columnInfos.Add(new GridColumnInfo("SHENHERID", "审核人ID", 100));
            columnInfos.Add(new GridColumnInfo("SHENHERXM", "审核人姓名", 100));
            columnInfos.Add(new GridColumnInfo("CAIJISJ", "采集时间", 100)
            {
                Formattype = FormatType.DateTime,
                Formatstring = "yyyy-MM-dd HH:mm:ss"
            });
            columnInfos.Add(new GridColumnInfo("SHENHESJ", "审核时间", 100)
            {
                Formattype = FormatType.DateTime,
                Formatstring = "yyyy-MM-dd HH:mm:ss"
            });
            columnInfos.Add(new GridColumnInfo("JIANYANXMMC", "检验项目名称", 100)
            {
                COL_READONLY = false
            });
            columnInfos.Add(new GridColumnInfo("TIAOMAHAO", "条码号", 100));
            columnInfos.Add(new GridColumnInfo("YANGBENLXMC", "样本类型", 100));
            columnInfos.Add(new GridColumnInfo("JIANYANZT", "检验状态", 100));
            columnInfos.Add(new GridColumnInfo("SHIYANJG", "检验结果", 100));
            columnInfos.Add(new GridColumnInfo("QUERENSJ", "采集确认时间", 100)
            {
                Formattype = FormatType.DateTime,
                Formatstring = "yyyy-MM-dd HH:mm:ss"
            });
            columnInfos.Add(new GridColumnInfo("JIESHOUSJ", "接收时间", 100)
            {
                Formattype = FormatType.DateTime,
                Formatstring = "yyyy-MM-dd HH:mm:ss"
            });
            columnInfos.Add(new GridColumnInfo("CAIJIQY", "采集点", 100));
            columnInfos.Add(new GridColumnInfo("RENYUANLX", "标本来源", 100));
            columnInfos.Add(new GridColumnInfo("SSKS", "所属科室", 200));
            columnInfos.Add(new GridColumnInfo("JIANYANFS", "检验方式", 80));
            columnInfos.Add(new GridColumnInfo("WAISONGBZ", "检验地点", 80)
            {
                COL_READONLY = false
            });
            #endregion
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            ShowLoading();
            try
            {
                QueryData();
            }
            finally
            {
                CloseLoading();
            }
        }

        private void QueryData()
        {
            NcovInfoParam param = new NcovInfoParam()
            {
                CHT_ID = teChtId.Text.Trim(),
                PAT_NM = tePatnm.Text.Trim(),
                QUERYTYPE = cbe_querytype.SelectedIndex,
                START_DT = de_start.DateTime.ToString("yyyy-MM-dd"),
                END_DT = de_end.DateTime.ToString("yyyy-MM-dd")
            };
            var ncovRecords = ncovBarConfirmService.QueryNcovInfo(param);
            gcData.DataSource = ncovRecords;
            var b = gvData;
            DevPublic.SetGridControlView(gvData, columnInfos, true, true);
        }

        private void FrmNcov_Load(object sender, EventArgs e)
        {
            ncovBarConfirmService = Globals.GetService<INcovBarConfirmService>();
            InitForm();
            de_start.Text = ncovBarConfirmService.GetCurrentDate("YYYY-MM-DD");
            de_end.Text = de_start.Text;
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (gvData.FocusedRowHandle >= 0)
            {
                NcovInfo ncovInfo = gvData.GetRow(gvData.FocusedRowHandle) as NcovInfo;
                //修改患者信息
                FrmModifyPat frmModifyPat = new FrmModifyPat(ncovInfo);
                if (frmModifyPat.ShowDialog() == DialogResult.OK)
                {
                    this.QueryData();
                }
            }
            else
            {
                Globals.ShowTips("请先选择需要修改的核酸抗体记录！");
            }
        }

        private void btnReportQuery_Click(object sender, EventArgs e)
        {
            Process.Start("http://chendd.work/wap/febrile/lisreport/");
        }

        private void OutExcel_Click(object sender, EventArgs e)
        {
            DataTable data = new DataTable();
            #region
            data.Columns.Add(new DataColumn("病历号码"));
            data.Columns.Add(new DataColumn("姓名"));
            data.Columns.Add(new DataColumn("出生日期"));
            data.Columns.Add(new DataColumn("性别"));
            data.Columns.Add(new DataColumn("年龄"));
            data.Columns.Add(new DataColumn("年龄单位"));
            data.Columns.Add(new DataColumn("身份证号"));
            data.Columns.Add(new DataColumn("家庭地址"));
            data.Columns.Add(new DataColumn("联系电话"));
            data.Columns.Add(new DataColumn("开单医生代码"));
            data.Columns.Add(new DataColumn("开单医生名称"));
            data.Columns.Add(new DataColumn("开单科室ID"));
            data.Columns.Add(new DataColumn("开单科室名称"));
            data.Columns.Add(new DataColumn("诊断名称"));
            data.Columns.Add(new DataColumn("主诉"));
            data.Columns.Add(new DataColumn("症状体征"));
            data.Columns.Add(new DataColumn("流行病学史"));
            data.Columns.Add(new DataColumn("就诊日期"));
            data.Columns.Add(new DataColumn("入院日期"));
            data.Columns.Add(new DataColumn("检测人ID"));
            data.Columns.Add(new DataColumn("检测人姓名"));
            data.Columns.Add(new DataColumn("审核人ID"));
            data.Columns.Add(new DataColumn("审核人姓名"));
            data.Columns.Add(new DataColumn("采集时间"));
            data.Columns.Add(new DataColumn("审核时间"));
            data.Columns.Add(new DataColumn("检验项目名称"));
            data.Columns.Add(new DataColumn("条码号"));
            data.Columns.Add(new DataColumn("样本类型"));
            data.Columns.Add(new DataColumn("检验状态"));
            data.Columns.Add(new DataColumn("检验结果"));
            data.Columns.Add(new DataColumn("采集确认时间"));
            data.Columns.Add(new DataColumn("接收时间"));
            data.Columns.Add(new DataColumn("采集点"));
            data.Columns.Add(new DataColumn("标本来源"));
            data.Columns.Add(new DataColumn("所属科室"));
            data.Columns.Add(new DataColumn("检验地点"));
            #endregion
            for (int i = 0; i < gvData.DataRowCount; i++)
            {
                List<NcovInfo> a = new List<NcovInfo>();
                DataRow row = data.NewRow();
                row["病历号码"] = gvData.GetRow(i).GetType().GetProperty("BINGRENID").GetValue(gvData.GetRow(i));
                row["姓名"] = gvData.GetRow(i).GetType().GetProperty("XINGMING").GetValue(gvData.GetRow(i));
                row["出生日期"] = gvData.GetRow(i).GetType().GetProperty("CHUSHENGRQ").GetValue(gvData.GetRow(i));
                row["性别"] = gvData.GetRow(i).GetType().GetProperty("XB").GetValue(gvData.GetRow(i));
                row["年龄"] = gvData.GetRow(i).GetType().GetProperty("NIANLING").GetValue(gvData.GetRow(i));
                row["年龄单位"] = gvData.GetRow(i).GetType().GetProperty("NLDW").GetValue(gvData.GetRow(i));
                row["身份证号"] = gvData.GetRow(i).GetType().GetProperty("SHENFENZH").GetValue(gvData.GetRow(i));
                row["家庭地址"] = gvData.GetRow(i).GetType().GetProperty("JIATINGDZ").GetValue(gvData.GetRow(i));
                row["联系电话"] = gvData.GetRow(i).GetType().GetProperty("DIANHUAHM").GetValue(gvData.GetRow(i));
                row["开单医生代码"] = gvData.GetRow(i).GetType().GetProperty("KAIDANYSID").GetValue(gvData.GetRow(i));
                row["开单医生名称"] = gvData.GetRow(i).GetType().GetProperty("KAIDANYSXM").GetValue(gvData.GetRow(i));
                row["开单科室ID"] = gvData.GetRow(i).GetType().GetProperty("KAIDANKSID").GetValue(gvData.GetRow(i));
                row["开单科室名称"] = gvData.GetRow(i).GetType().GetProperty("KAIDANKSMC").GetValue(gvData.GetRow(i));
                row["诊断名称"] = gvData.GetRow(i).GetType().GetProperty("ZHENDUANMC").GetValue(gvData.GetRow(i));
                row["主诉"] = gvData.GetRow(i).GetType().GetProperty("ZHUSU").GetValue(gvData.GetRow(i));
                row["症状体征"] = gvData.GetRow(i).GetType().GetProperty("TIGEJC").GetValue(gvData.GetRow(i));
                row["流行病学史"] = gvData.GetRow(i).GetType().GetProperty("JIANYAOBS").GetValue(gvData.GetRow(i));
                row["就诊日期"] = gvData.GetRow(i).GetType().GetProperty("JIUZHENRQ").GetValue(gvData.GetRow(i));
                row["入院日期"] = gvData.GetRow(i).GetType().GetProperty("ZHUYUANRQ").GetValue(gvData.GetRow(i));
                row["检测人ID"] = gvData.GetRow(i).GetType().GetProperty("JIANCERID").GetValue(gvData.GetRow(i));
                row["检测人姓名"] = gvData.GetRow(i).GetType().GetProperty("JIANCERXM").GetValue(gvData.GetRow(i));
                row["审核人ID"] = gvData.GetRow(i).GetType().GetProperty("SHENHERID").GetValue(gvData.GetRow(i));
                row["审核人姓名"] = gvData.GetRow(i).GetType().GetProperty("SHENHERXM").GetValue(gvData.GetRow(i));
                row["采集时间"] = gvData.GetRow(i).GetType().GetProperty("CAIJISJ").GetValue(gvData.GetRow(i));
                row["审核时间"] = gvData.GetRow(i).GetType().GetProperty("SHENHESJ").GetValue(gvData.GetRow(i));
                row["检验项目名称"] = gvData.GetRow(i).GetType().GetProperty("JIANYANXMMC").GetValue(gvData.GetRow(i));
                row["条码号"] = gvData.GetRow(i).GetType().GetProperty("TIAOMAHAO").GetValue(gvData.GetRow(i));
                row["样本类型"] = gvData.GetRow(i).GetType().GetProperty("YANGBENLXMC").GetValue(gvData.GetRow(i));
                row["检验状态"] = gvData.GetRow(i).GetType().GetProperty("JIANYANZT").GetValue(gvData.GetRow(i));
                row["检验结果"] = gvData.GetRow(i).GetType().GetProperty("SHIYANJG").GetValue(gvData.GetRow(i));
                row["采集确认时间"] = gvData.GetRow(i).GetType().GetProperty("QUERENSJ").GetValue(gvData.GetRow(i));
                row["接收时间"] = gvData.GetRow(i).GetType().GetProperty("JIESHOUSJ").GetValue(gvData.GetRow(i));
                row["采集点"] = gvData.GetRow(i).GetType().GetProperty("CAIJIQY").GetValue(gvData.GetRow(i));
                row["标本来源"] = gvData.GetRow(i).GetType().GetProperty("RENYUANLX").GetValue(gvData.GetRow(i));
                row["所属科室"] = gvData.GetRow(i).GetType().GetProperty("SSKS").GetValue(gvData.GetRow(i));
                row["检验地点"] = gvData.GetRow(i).GetType().GetProperty("WAISONGBZ").GetValue(gvData.GetRow(i));
                data.Rows.Add(row);
            }
            FolderBrowserDialog folder = new FolderBrowserDialog();
            if (folder.ShowDialog() == DialogResult.OK)
            {
                string ExportDir = folder.SelectedPath + "\\核酸统计报告" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
                int i = ExcelOut.DataTableToExcel(data, "测试", ExportDir, true);
                MessageBox.Show("成功导出" + i.ToString() + "条记录");
            }
        }
    }
}